<script src="<?= base_url()?>public/js/jquery.min.js"></script>
<div class="content-wrapper">
    <!-- 内容标题 -->
    <section class="content-header">
      <h1>
        <small>权限管理/</small> 设置权限
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i>系统设置</a></li>
        <li><a href="#">权限管理</a></li>
        <li class="active">设置权限</li>
      </ol>
    </section>
<br>
<form action="<?=base_url()?>index.php/admin/permis/permismodule/<?= $permis_id?>" method="post" id='PermisForm'>
<div class="box-body">
<table class='table table-bordered'>
    <thead>
        <tr style='background-color: #dedede;'>
            <th style='text-align: center;'><input type='checkbox' id='allbox'></th>
            <th nowrap style='text-align: center;'>权限</th>
            <th nowrap style='text-align: center;'>地址</th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($permis_module_list as $key=>$permis_module) {
            if(isset($permis_list[$permis_module['info']['url']])){
                $is_checked = 'checked';
            }else{
                $is_checked = '';
            }
        ?>
            <tr style='height: 28px;' pid="<?= $key?>">
                <td style='text-align: center;'>
                    <input type='checkbox' name='permis_module_id[]' <?=$is_checked?>
                        tag value="<?= $key?>"
                        parent_id="<?= $permis_module['info']['parent_id']?>" >
                    <input type='hidden' name='permis_module[<?= $key?>][url]'
                        value='<?= $permis_module['info']['url']?>'>
                </td>
                <td nowrap style='padding-left: 8px;'><?= $permis_module['info']['name']?></td>
                <td nowrap><?= $permis_module['info']['url']?></td>
            </tr>
        <?php
            if(isset($permis_module['list'])){
                foreach ($permis_module['list'] as $key=>$module) {
                    if(isset($permis_list[$module['info']['url']])){
                        $is_checked = 'checked';
                    }else{
                        $is_checked = '';
                    }
                    $left_width = '40';
                    $module['info']['url'] = strtr($module['info']['url'],['\\'=>'/']);
                    $namearr = explode('/', $module['info']['url']);
                    if(isset($namearr['2']) && 'index' != $namearr['2']){
                        $left_width = '70';
                    }
        ?>
                    <tr style='height: 28px;' pid="<?= $key?>">
                        <td style='text-align: center;'>
                            <input type='checkbox' name='permis_module_id[]' <?=$is_checked?>
                                tag value="<?= $key?>"
                                parent_id="<?= $module['info']['parent_id']?>">
                            <input type='hidden' name='permis_module[<?= $key?>][url]'
                                                            value='<?= $module['info']['url']?>'>
                        </td>
                        <td style='padding-left: <?=$left_width?>px;'><?= $module['info']['name']?></td>
                        <td style='padding-left: 10px;'><?= $module['info']['url']?></td>
                    </tr>
        <?php
                }
            }
        }
        ?>
    </tbody>
</table>
</div>
<div class="box-footer align-center">
    <button type="button" class="btn btn-primary btn-big bg-pink" id='submitBtn'>保存</button>
    <button type="button" class="btn btn-default btn-big" id='cancelBtn'>返回</button>
</div>
</form>

</div>
<script>
$(function(){
    //全选、取消全选
    $('#allbox').on('click',function(){
        $('input[type=checkbox][tag]').prop('checked',$(this).prop('checked'));
    });
    $('input[type=checkbox][tag]').on('click',function(){
        var parent_id = $(this).attr('parent_id');
        var pid = $(this).val();
        if(0 == parent_id){//一级模块
            // if(!$(this).prop('checked')){//取消一级模块，则一并取消所有二级模块权限
                $('input[type=checkbox][parent_id='+pid+']').prop('checked',$(this).prop('checked'));
            // }
        }else{//二级模块
            if($(this).prop('checked')){//选择二级模块后，需一并选择一级模块
                $('input[type=checkbox][value='+parent_id+']').prop('checked',$(this).prop('checked'));
            }
            if($(this).attr('list_id') && $(this).attr('list_id') > 0){//关联操作
                var list_id = $(this).attr('list_id');
                $(this).parent().parent().siblings('tr[pid='+list_id+']').
                    find('input[type=checkbox]').prop('checked',$(this).prop('checked'));
            }
        }
    });
    var is_submit = 1;
    $('#submitBtn').on('click',function(){
        var idx = 1;
        if(1 != is_submit){
            return false;
        }
        is_submit = 2;
        $('#PermisForm').submit();
    });
    $('#cancelBtn').on('click',function(){
        location.href = '../index';
    });
    //默认选中一级模块
    $('input[type=checkbox][tag]:checked').each(function(key,val){
        var parent_id = $(this).attr('parent_id');
        $('input[type=checkbox][value='+parent_id+']').prop('checked',$(this).prop('checked'));
    });
});
</script>
